function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

var productId = GetQueryString('Index');
$('.select2').select2();
$('#datepicker').datetimepicker({
    format: 'YYYY-MM-DD HH:mm:ss'

})
dataInit();

function dataInit() {
    $.ajax({
        type: "get",
        url: "http://www.puer.zone:30000/ProductDetailsInfo",
        async: true,
        data: {
            productId: productId
        },
        success: function (data) {
            DataView(data);
        }
    });

    $.ajax({
        type: "get",
        url: "http://www.puer.zone:30000/ActivityName",
        async: true,
        data: {},
        success: function (data) {
            var box = $("#pro_act");
            box.empty();
            for (var i = 0; i < data.length; i++) {
                var text = `<option value='${data[i]}'>${data[i]}</option>`;
                box.append(text);
            }
        }
    });
}

//图片显示
function pic_replace(dom1, dom2) {
    var file = document.getElementById(dom1).files;
    var reader = new FileReader();
    reader.readAsDataURL(file[0]);
    reader.onload = function (e) {
        var Img = '<img class="img_sty" src="' + this.result + '" width="85px" height="100px"/>';
        $('.' + dom2).html(Img);
    }
}

function DataView(data) {
    var inpText = $('.new_item').find('input');
    for (var i = 0; i < inpText.length; i++) {
        inpText.eq(i).val(data[0][i]);
    }

    $('#pro_type').val(data[0][6]).trigger('change');
    $('#group1').val(data[0][7][0]).trigger('change');
    $('#group2').val(data[0][7][1]).trigger('change');
    $('#pro_act').val(data[0][8]).trigger('change');

    for (var i = 0; i < data[0][9].length; i++) {
        var text = `<tr>
		      		    		<td>${data[0][9][i][0]}</td>
		      		    		<td>${data[0][9][i][1]}</td>
		      		    		<td>
		      		    			${data[0][9][i][2]}
		      		    		</td>
		      		    	</tr>`;
        $('.format_data').append(text);
    }

    for (var i = 0; i < data[1].length; i++) {
        $('.img').eq(i).html('<img src="' + data[1][i] + '" width="85px" height="100px" style="vertical-align: top;">');
    }

    $('.detail_pic').html('<img src="' + data[0][10] + '" width="85px" height="100px" style="vertical-align: top;">');
    $('.file_img1').html('<img src="' + data[0][11] + '" width="85px" height="100px" style="vertical-align: top;">');
}

$('.submit_all').click(function () {
    var textOnoff = true;

    //判断必填信息
    if ($('input[name=Name]').val() == '' && textOnoff == true) {
        textOnoff = false;
        layer.msg('请填写商品名称')

    } else if ($('input[name=Price]').val() == '' && textOnoff == true) {
        textOnoff = false;
        layer.msg('请填写商品价格')

    } else if ($('input[name=referPrice]').val() == '' && textOnoff == true) {
        textOnoff = false;
        layer.msg('请填写市场参考价格')

    } else if ($('input[name=Brand]').val() == '' && textOnoff == true) {
        textOnoff = false;
        layer.msg('请填写商品品牌')

    } else if ($('#group1').val() == '' && $('#group2').val() == '' && textOnoff == true) {
        textOnoff = false;
        layer.msg('请选择商品分组')

    } else if ($('.file_img1').text() == '+' && textOnoff == true) {
        layer.msg('请添加商品缩略图');
        textOnoff = false;
    } else if ($('.detail_pic').text() == '+' && textOnoff == true) {
        layer.msg('请添加商品详情介绍图');
        textOnoff = false;
    } else {

        for (var i = 0; i < $('.img').length; i++) {
            if ($('.img').eq(i).text() == '+') {
                layer.msg('请添加商品轮播图至5张');
                textOnoff = false;
            }
        }

    }

    if (textOnoff) {
        var pic_num = 0;//轮播图数量
        for (var i = 0; i < $('.img').length; i++) {
            if ($('.img').eq(i).find('img').length == 1) {
                pic_num += 1;
            }
        }

        var formData = new FormData(document.getElementById('new_form3'));
        formData.append('ProductId', productId);
        formData.append('ProductName', $('input[name=Name]').val());
        formData.append('ProductPrice', $('input[name=Price]').val());
        formData.append('ReferPrice', $('input[name=referPrice]').val());
        formData.append('ProductBrand', $('input[name=Brand]').val());
        formData.append('ProductType', $('#pro_type').val());
        formData.append('ProductGroup1', $('#group1').val());
        formData.append('ProductGroup2', $('#group2').val());
        formData.append('ProductActive', $('#pro_act').select2('val').join('_'));
        formData.append('ImageNumber', pic_num);
        formData.append('Image1', $("#file_pic1").val());

        $.ajax({
            type: "POST",
            url: "http://www.puer.zone:30000/UpdateProduct/",
            async: true,
            data: formData,
            processData: false,
            contentType: false,
            success: function (data) {
                if (data == 'success') {
                    layer.msg('提交成功', {time: 500}, function () {
                        window.location.href = "http://www.puer.zone:9000/product_list.html";
                    });
                } else if (data[1] == 'name') {
                    layer.msg('商品名称重复');
                } else {
                    layer.msg('提交失败');
                }
            }
        });
    }

})

function group1Change() {
    var man = ['套西', '单西', '裤子', '长袖衬衫', '短袖衬衫', '马甲', '大衣', '风衣', '夹克', '中华立领', '中式'];
    var child = ['套西', '单西', '裤子', '长袖衬衫', '短袖衬衫', '马甲', '大衣', '风衣'];
    var work = ['男士西装', '男士衬衫', '男士裤子', '男士大衣', '男士夹克', '女士西服', '女士衬衣', '女士短裙', '大衣', '风衣'];
    var group2 = [];
    var group1 = $("#group1").val();
    if ((group1 == "男装") || (group1 == "女装")) {
        group2 = man;
    } else if (group1 == "童装") {
        group2 = child;
    } else if (group1 == "职业装") {
        group2 = work;
    }

    $("#group2").empty();
    for (var i = 0; i < group2.length; i++) {
        var text = `<option value='${group2[i]}'>${group2[i]}</option>`;
        $("#group2").append(text);
    }
}
	